package com.youlai.boot.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.app.model.AppAreaVO;
import com.youlai.boot.modules.app.model.HomeVO;
import com.youlai.boot.modules.app.service.AppService;
import com.youlai.boot.modules.category.model.CategoryVO;
import com.youlai.boot.modules.category.mapper.CategoryMapper;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import com.youlai.boot.modules.banner.service.BannerService;
import com.youlai.boot.modules.bfapp.model.query.BfAppQuery;
import com.youlai.boot.modules.bfapp.model.vo.BfAppVO;
import com.youlai.boot.modules.bfapp.service.BfAppService;
import com.youlai.boot.modules.area.service.AreaService;
import com.youlai.boot.modules.banner.model.BannerVO;
import com.youlai.boot.modules.banner.model.BannerPageQuery;
import com.youlai.boot.modules.area.model.AreaPageQuery;
import com.youlai.boot.modules.area.model.AreaVO;
import com.youlai.boot.modules.area.model.AreaProductVO;
import com.youlai.boot.modules.area.service.AreaProductService;

import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AppServiceImpl implements AppService {

    private final CategoryMapper categoryMapper;

    private final BannerService bannerService;

    private final AreaService areaService;

    private final AreaProductService areaProductService;

    private final BfAppService bfAppService;

    public HomeVO home() {
        // 获取轮播图列表
        List<BannerVO> banners = this.listBanners();

        // 获取首页二级分类列表
        List<CategoryVO> categories = categoryMapper.getHomeCategoryList();

        // 获取区域列表
        List<AppAreaVO> areas = this.listAreas();

        // 组装返回数据
        HomeVO homeVO = new HomeVO();
        homeVO.setBanners(banners);
        homeVO.setCategories(categories);
        homeVO.setAreas(areas);
        homeVO.setVersion(appVersion());
        return homeVO;
    }

    public BfAppVO getVersion() {
        return appVersion();
    }

    private List<BannerVO> listBanners() {
        BannerPageQuery query = new BannerPageQuery();
        query.setShowFlag(1);
        query.setType(1);
        query.setPageNum(1);
        query.setPageSize(16);
        return bannerService.getBannerPage(query).getRecords();
    }

    private List<AppAreaVO> listAreas() {
        AreaPageQuery query = new AreaPageQuery();
        query.setShowFlag(1);
        query.setPageNum(1);
        query.setPageSize(999);
        return areaService.getAreaPage(query).getRecords().stream()
                .map(this::convertToAppAreaVO)
                .collect(Collectors.toList());
    }

    private AppAreaVO convertToAppAreaVO(AreaVO area) {
        AppAreaVO vo = new AppAreaVO();
        BeanUtils.copyProperties(area, vo);

        // 查询该区域关联的banner
        BannerPageQuery bannerQuery = new BannerPageQuery();
        bannerQuery.setType(2); // banner.type == 2
        bannerQuery.setAreaId(area.getId()); // banner.area_id == area.id
        bannerQuery.setShowFlag(1); // 只查询显示的banner
        List<BannerVO> banners = bannerService.getBannerPage(bannerQuery).getRecords();
        vo.setBanners(banners);

        // 查询该区域关联的产品
        List<AreaProductVO> products = areaProductService.listByAreaId(area.getId());
        vo.setProducts(products);

        return vo;
    }

    private BfAppVO appVersion() {
        BfAppVO bfAppVO = null;
        List<BfAppVO> bfAppVOs = null;

        BfAppQuery query = new BfAppQuery();
        query.setPageNum(1);
        query.setPageSize(1);

        bfAppVOs = bfAppService.getBfAppPage(query).getRecords();
        if (bfAppVOs.size() == 1) {
            bfAppVO = bfAppVOs.get(0);
        }
        
        return bfAppVO;
    }
}